<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%contract}}`.
 * Has foreign keys to the tables:
 *
 * - `{{%contractor}}`
 */
class m220127_103616_create_contract_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('{{%contract}}', [
            'id' => $this->primaryKey(),
            'own_number' => $this->integer()->unique(),
            'contractor_number' => $this->integer(),
            'id_contractor' => $this->integer()->notNull(),
            'subject' => $this->string(512),
            'total_price' => $this->money(),
            'date_sign' => $this->date(),
            'date_realize' => $this->date(),
            'comment' => $this->text(),
        ]);

        // creates index for column `id_contractor`
        $this->createIndex(
            '{{%idx-contract-id_contractor}}',
            '{{%contract}}',
            'id_contractor'
        );

        // add foreign key for table `{{%contractor}}`
        $this->addForeignKey(
            '{{%fk-contract-id_contractor}}',
            '{{%contract}}',
            'id_contractor',
            '{{%contractor}}',
            'id',
            'CASCADE'
        );
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // drops foreign key for table `{{%contractor}}`
        $this->dropForeignKey(
            '{{%fk-contract-id_contractor}}',
            '{{%contract}}'
        );

        // drops index for column `id_contractor`
        $this->dropIndex(
            '{{%idx-contract-id_contractor}}',
            '{{%contract}}'
        );

        $this->dropTable('{{%contract}}');
    }
}
